from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData
from django.conf import settings
from apps.users.models import User
def generate_email_verify_url(user):
    """
    生成邮箱激活链接
    生成序列化器
    :return:
    """
    #创建序列化器对象
    s = Serializer(settings.SECRET_KEY, 3600*24 )

    #准备需要序列化的数据
    data = {'user_id':user.id, 'email':user.email}

    #序列化
    token = s.dumps(data).decode()

    #返回响应数据
    verify_url = settings.EMAIL_VERIFY_URL + token

    return verify_url

def check_email_verify_url(token):
    """
    反序列化
    :param token: 用户信息密文
    :return: 用户信息
    """
    s = Serializer(settings.SECRET_KEY, 3600*24)

    try:
        data = s.loads(token)
    except BadData:
        return None
    else:
        #提取用户信息
        user_id = data.get('user_id')
        email = data.get('email')

        #使用user_id和email查询对应的用户对象
        try:
            user = User.objects.get(id=user_id, email=email)
        except User.DoesNotExist:
            return None
        else:
            return user
